<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests;

use Illuminate\Support\Facades\Auth;

/**
 * 数据字典选项模块表单验证
 * Class DictionaryItemsRequest
 * @package App\Http\Requests
 */
class DictionaryItemsRequest extends BaseRequest
{

    /**
     * 注入表单请求验证时自动校验
     *
     * @var bool
     */
    public $autoValidate = false;

    /**
     * 用户是否有权操作，true是，false否
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 字段验证规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => 'required|integer',
            'did' => 'required|integer',
            'created_user_id' => 'required|integer',
            'name' => 'required|min:1|max:50',
            'value' => 'required|min:1|max:200',
            'status' => 'required|boolean',
            'sort' => 'required|integer',
            'sort.*.id' => 'required',
            'sort.*.sort' => 'required|integer',
            'description' => 'nullable|max:200'
        ];
    }

    /**
     * 提示信息
     * @return array
     */
    public function messages()
    {
        return [
        ];
    }

    /**
     * 定义场景
     * @var \string[][]
     */
    public $scenes = [
        'store' => ['created_user_id','did', 'name', 'value', 'description', 'sort', 'status'],
        'update' => ['name', 'value', 'description', 'sort', 'status'],
        'destroy' => ['id'],
        'updateStatus' => ['status'],
        'updateSort' => ['sort.*.id', 'sort.*.sort']
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => __('common.id'),
            'did' => __('dictionary-items.did'),
            'name' => __('dictionary-items.name'),
            'value' => __('dictionary-items.value'),
            'description' => __('dictionary-items.description'),
            'sort' => __('common.sort'),
            'status' => __('common.status'),
            'sort.*.id' => __('common.id'),
            'sort.*.sort' => __('common.sort'),
        ];
    }

    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->offsetSet('created_user_id', Auth::id());
        $this->offsetSet('did', $this->dictionary->id);
    }
}
